const app = getApp()
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    multiIndex: [0, 0],
    multiArray: [['6月10日', '7月10日', '8月10日', '12月10日'], ['10', '11', '12', '13', '14']],
    multiIndex2: [0, 0],
    multiArray2: [['6月10日', '7月10日', '8月10日', '12月10日'], ['10', '11', '12', '13', '14']],
    imgUrls: ['../../img/no/banner2.png', '../../img/no/banner2.png'],
    startTime:'',
    startHour:'',
    endTime:'',
    endHour:'',
    day:1,
    isAble:true
  },

  // 选择开始时间
  bindMultiPickerChange: function (e) {
    
    
    this.setData({
      multiIndex: e.detail.value
    })
    let dayIndex = e.detail.value[0]
    let hourIndex = e.detail.value[1]
    var hour=this.data.multiArray[1][hourIndex]
    hour = hour.substring(0, 2)
    this.setData({
      startTime: this.data.multiArray[0][dayIndex],
      startHour: parseFloat(hour)
    })
    
    var now = new Date();
    var startTime = new Date(this.data.startTime)
    if(startTime.getMonth()==now.getMonth()&&startTime.getDay()==now.getDay()&&now.getHours()>this.data.startHour){
      wx.showToast({
        title: '取车时间小于当前时间',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if(!util.isBlank(this.data.endTime)){
      if (this.data.endTime > this.data.startTime) {
        var start = new Date(this.data.startTime)
        var end = new Date(this.data.endTime)
        var cha = end.getTime() - start.getTime();
        var day = Math.floor(cha / (24 * 3600 * 1000))+1
        if (this.data.startHour >= this.data.endHour) {
             day = day - 1
        }
        var totalPrice = parseFloat(this.data.product.price) * day
        this.setData({
          day: day,
          totalPrice: totalPrice.toFixed(2)
        })
      } else if (this.data.endTime == this.data.startTime) {
        if (this.data.startHour >= this.data.endHour) {
          wx.showToast({
            title: '还车时间不能小于或者等于取车时间',
            icon: 'none',
            duration: 1500
          })
          return
        } else {
          this.setData({
            day: 1
          })
        }
      } else {
        wx.showToast({
          title: '还车时间不能小于或者等于取车时间',
          icon: 'none',
          duration: 1500
        })
        return
      }
    }
    this.setData({
      isAble: false
    })

  },

  // 选择截止时间
  bindMultiPickerChange2: function (e) {
    this.setData({
      multiIndex2: e.detail.value
    })
    let dayIndex = e.detail.value[0]
    let hourIndex = e.detail.value[1]
    var hour = this.data.multiArray2[1][hourIndex]
    hour = hour.substring(0, 2)
    this.setData({
      endTime: this.data.multiArray2[0][dayIndex],
      endHour: parseFloat(hour)
    })
    if (this.data.endTime > this.data.startTime){
      var start = new Date(this.data.startTime)
      var end = new Date(this.data.endTime)
      var cha = end.getTime() - start.getTime();
      var day = Math.floor(cha / (24 * 3600 * 1000))+1
      if (this.data.startHour >= this.data.endHour){
          day=day-1     
      }
      var totalPrice = parseFloat(this.data.product.price)*day
      this.setData({
        day: day,
        totalPrice: totalPrice.toFixed(2)
      })
    } else if (this.data.endTime == this.data.startTime) {
      if (this.data.startHour >= this.data.endHour){
        wx.showToast({
          title: '还车时间不能小于或者等于取车时间',
          icon: 'none',
          duration: 1500
        })
        return
      }else{
        this.setData({
          day:1
        })
      }
    }else{
      wx.showToast({
        title: '还车时间不能小于或者等于取车时间',
        icon: 'none',
        duration: 1500
      })
      return
    }
  },

  // 跳转到订单
  toOrder: function () { 
    if (util.isBlank(this.data.startTime)){
      wx.showToast({
        title: '请选择取车时间',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if (util.isBlank(this.data.endTime)) {
      wx.showToast({
        title: '请选择还车时间',
        icon: 'none',
        duration: 1500
      })
      return
    }
    if (this.data.endTime == this.data.startTime) {
      if (this.data.startHour >= this.data.endHour) {
        wx.showToast({
          title: '还车时间不能小于或者等于取车时间',
          icon: 'none',
          duration: 1500
        })
        return
      } 
    } else if (this.data.endTime < this.data.startTime){
      wx.showToast({
        title: '还车时间不能小于或者等于取车时间',
        icon: 'none',
        duration: 1500
      })
      return
    }
    var now = new Date();
    var startTime = new Date(this.data.startTime)
    if (startTime.getMonth() == now.getMonth() && startTime.getDay() == now.getDay() && now.getHours() > this.data.startHour) {
      wx.showToast({
        title: '取车时间小于当前时间',
        icon: 'none',
        duration: 1500
      })
      return
    }
    wx.navigateTo({
      url: '../order/order?startTime=' + this.data.startTime + '&endTime=' + this.data.endTime + '&day=' + this.data.day + '&startHour=' + this.data.startHour + '&endHour=' + this.data.endHour + '&productId=' + this.data.productId + '&startCityId=' + 324 + '&endCityId=' + 324 +'&startCityName=海口&endCityName=海口&type=1'
    })
  },

  //修复swiper高度不能自适应问题
  imageLoad: function (e) {
    var res = wx.getSystemInfoSync();
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      ratio = imgwidth / imgheight;
    this.setData({
      bannerHeight: res.windowWidth / ratio
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      productId: options.productId,
      userInfo: app.globalData.userInfo,
      couponValue: app.globalData.couponValue
    })
    this.setDate()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.loadProduct()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '琼崖租车',
      path: '/pages/index/index'
    }
  },
  loadProduct: function () {
    let that = this
    wx.request({
      url: 'https://small.qiongyazuche.com/sunday/small/product/findOne',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        'productId': that.data.productId
      },  
      success: function (res) {
        that.setData({
          product: res.data.result,
          totalPrice: res.data.result.price.toFixed(2)
        })
      }
    })
  },
  setDate:function(){
    let that = this
    Date.prototype.Format = function (fmt) { //author: meizz   
      var o = {
        "M+": this.getMonth() + 1,                 //月份   
        "d+": this.getDate(),                    //日   
        "h+": this.getHours(),                   //小时   
        "m+": this.getMinutes(),                 //分   
        "s+": this.getSeconds(),                 //秒   
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
        "S": this.getMilliseconds()             //毫秒   
      };
      if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
      return fmt;
    }
    var date = new Date();

    //当前时间的毫秒
    var utc = date.getTime();
 
    //循环30天，
    let datearray = new Array()
    var hours=date.getHours()
    let numberarray = []
    for(var i=1;i<=24;i++){
      numberarray.push(i+':00')
    }
    let totalarray = new Array()
    for (var i = 0; i <= 60; i++) {

      var _utc = utc + 1000 * 60 * 60 * 24 * i;
      date.setTime(_utc);
      //格式化的后的时间
      var _date = date.Format('yyyy-MM-dd');
      datearray.push(_date)
      //console.info(_date);
    }
    var today = new Date();
    var _today = today.Format('yyyy-MM-dd');
    totalarray.push(datearray)
    totalarray.push(numberarray)
    that.setData({
      multiArray: totalarray,
      multiArray2: totalarray
    })
  }
})